package org.javaboy.meeting.controller;

import org.javaboy.meeting.model.MeetingRoom;
import org.javaboy.meeting.service.MeetingRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class MeetingRoomController {

    @Autowired
    MeetingRoomService meetingRoomService;

    /**
     * 查看会议功能
     * @param model
     * @return
     */
    @RequestMapping({"/meetingrooms","/admin/meetingrooms"})
    public String meetingrooms(Model model){
        model.addAttribute("mrs",meetingRoomService.getAllMr());
        return "meetingrooms";
    }

    /**
     * 查看会议室详情
     * @param roomid
     * @param model
     * @return
     */
    @RequestMapping("/roomdetails")
    public String roomdetails(Integer roomid,Model model){
        model.addAttribute("mr",meetingRoomService.getMrById(roomid));
        return "roomdetails";
    }

    /**
     * 更新会议室
     * @param meetingRoom
     * @return
     */
    @RequestMapping("/updateroom")
    public String updateRoom(MeetingRoom meetingRoom){
        Integer result = meetingRoomService.updateRoom(meetingRoom);
        System.out.println(meetingRoom);
//        if(result == 1){
//            return "redirect:/meetingrooms";
//        }else {
////            return "forward:/roomdetails";
//        }
        return "redirect:/meetingrooms";

    }

    /**
     * 返回添加会议室内容
     * @return
     */
    @RequestMapping("/admin/addmeetingroom")
    public String addmeetingroom(){
        return "addmeetingroom";
    }

    /**
     *添加会议室
     * @param meetingRoom
     * @return
     *
     */
    @RequestMapping("/admin/doAddMr")
    public String doAddMr(MeetingRoom meetingRoom,Model model){
        System.out.println(meetingRoom.getRoomnum());
        if (meetingRoom.getRoomnum()>Integer.MAX_VALUE||meetingRoom.getRoomnum()<0){
            model.addAttribute("error","会议室不规范");
            return "forward:/addmeetingroom";
        }else {
            Integer result = meetingRoomService.addMr(meetingRoom);
            return "redirect:/meetingrooms";
        }
    }
}
